package com.intermancer.trader.signal;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.intermancer.predictor.data.ConsumeResponse;
import com.intermancer.predictor.data.Quantum;

public class ChannelDifferenceTSS implements TradeSignalStrategy {
	
	public static final double DEFAULT_ENTER_LONG_THRESHOLD = 1.0d;
	public static final double DEFAULT_EXIT_LONG_THRESHOLD = 0.0d;
	public static final double DEFAULT_ENTER_SHORT_THRESHOLD = -1.0d;
	public static final double DEFAULT_EXIT_SHORT_THRESHOLD = 0.0d;
	
	UUID positionId;
	boolean longPosition;
	private double enterLongThreshold = DEFAULT_ENTER_LONG_THRESHOLD;
	private double exitLongThreshold = DEFAULT_EXIT_LONG_THRESHOLD;
	private double enterShortThreshold = DEFAULT_ENTER_SHORT_THRESHOLD;
	private double exitShortThreshold = DEFAULT_EXIT_SHORT_THRESHOLD;

	@Override
	public List<TradeSignal> getTradeSignals(ConsumeResponse consumeResponse,
			Quantum quantum) {
		List<TradeSignal> tradeSignals = new ArrayList<TradeSignal>();
		if(ConsumeResponse.CONSUME_COMPLETE.equals(consumeResponse)) {
			Double val1 = quantum.getChannel(-1).getValue();
			Double val2 = quantum.getChannel(-2).getValue();
			double difference = val1 - val2;
			if ((difference < getExitLongThreshold() && isLong())
					|| (difference > getExitShortThreshold() && isShort())) {
				clearOutExistingPosition(tradeSignals);
			}
			if (difference > getEnterLongThreshold() && !isLong()) {
				enterLongPosition(tradeSignals);
			} else if (difference < getEnterShortThreshold() && !isShort()) {
				enterShortPosition(tradeSignals);
			}
		}
		return tradeSignals;
	}
	
	private void enterShortPosition(List<TradeSignal> tradeSignals) {
		enterPosition(tradeSignals, TradeSignalType.SHORT);
	}

	private void enterLongPosition(List<TradeSignal> tradeSignals) {
		enterPosition(tradeSignals, TradeSignalType.LONG);
	}

	private void enterPosition(List<TradeSignal> tradeSignals, 
			TradeSignalType tradeSignalType) {	
		positionId = UUID.randomUUID();
		if (TradeSignalType.LONG.equals(tradeSignalType)) {
			longPosition = true;
		} else {
			longPosition = false;
		}
		
		BaseTradeSignal enterSignal = new BaseTradeSignal();
		enterSignal.setTradeSignalType(tradeSignalType);
		enterSignal.setId(positionId);
		tradeSignals.add(enterSignal);
	}

	private void clearOutExistingPosition(List<TradeSignal> tradeSignals) {
		BaseTradeSignal exitSignal = new BaseTradeSignal();
		exitSignal.setTradeSignalType(TradeSignalType.EXIT);
		exitSignal.setId(UUID.randomUUID());
		exitSignal.setReferenceTradeSignalId(positionId);
		tradeSignals.add(exitSignal);
		
		positionId = null;
	}

	@Override
	public boolean isLong() {
		return positionId != null && longPosition;
	}
	
	@Override
	public boolean isShort() {
		return positionId != null && !longPosition;
	}

	@Override
	public void init() {
		positionId = null;
	}

	public double getEnterLongThreshold() {
		return enterLongThreshold ;
	}

	public void setEnterLongThreshold(double enterLongThreshold) {
		this.enterLongThreshold = enterLongThreshold;
	}

	public double getExitLongThreshold() {
		return exitLongThreshold;
	}

	public void setExitLongThreshold(double exitLongThreshold) {
		this.exitLongThreshold = exitLongThreshold;
	}

	public double getEnterShortThreshold() {
		return enterShortThreshold;
	}

	public void setEnterShortThreshold(double enterShortThreshold) {
		this.enterShortThreshold = enterShortThreshold;
	}

	public double getExitShortThreshold() {
		return exitShortThreshold;
	}

	public void setExitShortThreshold(double exitShortThreshold) {
		this.exitShortThreshold = exitShortThreshold;
	}

}
